mwu
Statistics Timm Feb10 Revised

Mann-Whitney  Tests (paired)

Problem
=======

Check if the ranks found in two populations are significantly different.

Solution
========

 function mwu(x,pop1,pop2,up,critical,                 \
	      i,data,ranks,n,n1,sum1,ranks1,n2,sum2,ranks2, \
	      correction,meanU,sdU,z) 
 {
     for(i in pop1) data[++n]=pop1[i]
     for(i in pop2) data[++n]=pop2[i]
     rank(data,ranks)
     for(i in pop1) { n1++; sum1 += ranks1[i] = ranks[pop1[i]] }
     for(i in pop2) { n2++; sum2 += ranks2[i] = ranks[pop2[i]] }
     #
     meanU      = n1*(n1+n2+1)/2;  # symmetric, just use pop1's z
     sdU        = (n1*n2*(n1+n2+1)/12)^0.5
     correction = sum1 > meanU ? -0.5 : 0.5  
     z          = abs((sum1 - meanU + correction )/sdU)
     # 
     if (z >= 0 && z <= critical) 
	 return 0
     if (up) 
	 return median(ranks1,n1) - median(ranks2,n2) 
     else
	 return median(ranks2,n2) - median(ranks1,n1) 
 }

   function Mwu() {
       print "1,  1 " mwu1(1,1)
       print "0.5,1 " mwu1(0.5,1)
       print "2,  1 " mwu1(2,1)
       print "1,  0 " mwu1(1,0)
       print "0.5,0 " mwu1(0.5,0)
       print "2,  0 " mwu1(2,0)
   }
   function mwu1(mult,up,   pop1,pop2,out,i) {
       mult = mult ? mult : 1
       s2a("1 4.6 2 4.7 3 4.9 "	\
	   "4 5.1 5 5.2 6 5.5 "	 \
	   "7 5.8 8 6.1 9 6.5 "	  \
	   "10 6.5 11 7.2",pop1," ")
       s2a("1 5.2 2 5.3 3 5.4 "	 \
	   "4 5.6 5 6.2 6 6.3 "	  \
	   "7 6.8 8 7.7 9 8.0 "	   \
	   "10 8.1", pop2," ")
       for(i in pop1) pop1[i] *= mult
       out = mwu("a",pop1,pop2,up,criticalValue(95))
       if (out == 0)
	   return "tie"
       else if (out > 0)
	   return "win for a"
       else if (out < 0)
	   return "loss for a"
   }

Author
======

Tim Menzies
			       
